using System;
using System.Threading;
using Cysharp.Threading.Tasks; // 引入UniTask命名空间
using UnityEngine;

namespace Cysharp.Threading.Tasks {
    public class Timer {

        /// <summary>
        /// 记录当前已经经过的时间
        /// </summary>
        private float currentTime;
        /// <summary>
        /// 计时器的总时间
        /// </summary>
        private float duration;
        /// <summary>
        /// 间隔时间 秒
        /// </summary>
        private float delayTime;
        /// <summary>
        /// 计时器是否正在运行
        /// </summary>
        private bool isRunning;
        /// <summary>
        /// 计时器是否为倒数计时器 -- 暂未实现
        /// </summary>
        private bool isCountdown;
        /// <summary>
        /// 取消Token的Source
        /// </summary>
        private CancellationTokenSource cts;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="duration">计时器的总时间</param>
        /// <param name="delayTime">间隔时间 秒</param>
        public Timer(float duration, float delayTime = 0.1f) {
            this.Duration = duration;
            this.DelayTime = delayTime;
            IsRunning = false;
            // 初始化 CancellationTokenSource
            cts = new CancellationTokenSource();
        }

        /// <summary>
        /// 当前已经经过的时间
        /// </summary>
        public float CurrentTime { get => currentTime; set => currentTime = value; }
        /// <summary>
        /// 计时器的总时间
        /// </summary>
        public float Duration { get => duration; set => duration = value; }
        /// <summary>
        /// 间隔时间 秒
        /// </summary>
        public float DelayTime { get => delayTime; set => delayTime = value; }
        /// <summary>
        /// 计时器是否正在运行
        /// </summary>
        public bool IsRunning { get => isRunning; set => isRunning = value; }
        /// <summary>
        /// 计时器是否为倒数计时器 -- 暂未实现
        /// </summary>
        public bool IsCountdown { get => isCountdown; set => isCountdown = value; }

        /// <summary>
        /// 计时器的Tick事件
        /// </summary>
        public event Func<UniTask> OnTimerTick;
        /// <summary>
        /// 计时器完成事件
        /// </summary>
        public event Func<UniTask> OnTimerComplete;

        /// <summary>
        /// 开始计时器
        /// </summary>
        public void Start() {
            if (IsRunning) return;
            IsRunning = true;
            CurrentTime = 0f;
            // 开始执行计时器的循环
            _ = TimerLoop();
        }

        /// <summary>
        /// 停止计时器
        /// </summary>
        public void Stop() {
            if (!IsRunning) return;
            IsRunning = false;
            // 取消循环中的 await UniTask.Delay()，中断循环
            cts.Cancel();
        }

        /// <summary>
        /// 计时器循环
        /// </summary>
        private async UniTask TimerLoop() {
            while (CurrentTime < Duration && IsRunning) {
                // 等待 delayTime 秒
                await UniTask.Delay(TimeSpan.FromSeconds(delayTime), cancellationToken: cts.Token);
                // 更新当前已经经过的时间
                CurrentTime += delayTime;
                // 如果有注册 Tick 事件，调用事件
                if (OnTimerTick != null) await OnTimerTick.Invoke();
            }

            // 如果计时器是仍在运行中，并且有注册 Complete 事件，调用事件
            if (IsRunning && OnTimerComplete != null) await OnTimerComplete.Invoke();

            // 计时器运行结束
            IsRunning = false;
        }
    }
}